package com.sumainfor.pc.gmio.controller.tenant;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.gmio.feign.extend.HouseFeign;
import com.sumainfor.pc.gmio.service.impl.tenant.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 * 收入相关账单
 * @Auther: zhlu
 * @Date: 2019/3/24 13:22
 * @Description:
 */
@RestController
@RequestMapping("/tenant/key/bill")
public class BillController {

    @Autowired
    BillService BillServicel;

    @Autowired
    HouseFeign HouseFeign;

    /**
     * 功能描述: 
     * 根据合同编号获取当前租客首月房租账单
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/24 13:29
     */
    @GetMapping("/getFirsMonRent")
    public JsonResult getFirsMonRent(@RequestParam Map<String,Object> params)throws Exception{
        JsonResult result=new JsonResult();
        params.put("BiType","1");//房租
        params.put("DepositType","1");//房租类型   1.房租房租押金   2.水电押金   (账单类型为1)
        params.put("FirstMon","1");//房租账单是否是首月    0否   1.是
        params.put("delfg","0");
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"PaId"})){
            result.putFailed("PaId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"uopid"})){
            result.putFailed("uopid不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"remoteIp"})){
            result.putFailed("remoteIp不能为空！");
            return  result;
        }
        result.put(BillServicel.getFirsMonRent(params));
        return result;
    }

    /**
     * 功能描述: 
     * 支付完成验证与操作
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/27 14:53
     */
    @ResponseBody
    @PostMapping("/PaymentCompleted")
    public JsonResult PaymentCompleted(@RequestBody Map<String,Object>params)throws Exception{
        JsonResult result =new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"BiId"})){
            result.putFailed("BiId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"TenId"})){
            result.putFailed("TenId不能为空！");
            return  result;
        }
        return BillServicel.WechatPaymentCompleted(params);
    }

    /**
     * 功能描述: 
     * 根据合同编号获取当前合同下的首月房租账单和水电账单
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/29 10:21
     */
    @GetMapping("/getBillMonRentList")
    public JsonResult getBillMonRentList(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        params.put("BiType","1");//账单类型  1.房租（合同） 2.公共事业费  3.维修费用 4.延期退审核  5.其他费用  6.退租账单 7.夜间开门
        params.put("FirstMon","1");//房租账单是否是首月    0否   1.是
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"PaId"})){
            result.putFailed("PaId不能为空！");
            return  result;
        }
        result.put(BillServicel.getBillMonRentList(params));
        return result;
    }

    /**
     * 功能描述: 
     * 支付宝转账
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/29 21:02
     */
    @ResponseBody
    @PostMapping("/PayOffline")
    public JsonResult PayOffline(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        params.put("BiType","1");//房租
        params.put("DepositType","2");//房租类型   1.房租房租押金   2.水电押金   (账单类型为1)
        params.put("FirstMon","1");//房租账单是否是首月    0否   1.是
        params.put("delfg","0");
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"PaId"})){
            result.putFailed("PaId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"TenId"})){
            result.putFailed("TenId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"PayFile"})){
            result.putFailed("PayFile不能为空！");
            return  result;
        }
        return BillServicel.PayOffline(params);
    }



    /**
     * 功能描述: 
     * 根据业主合同编号查询业主全部账单总数
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/1 10:07
     */
    @GetMapping("/getOwnerBillAllCount")
    public JsonResult getOwnerBillAllCount(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        params.put("delfg","0");
        Integer getOwnerBillAllCount=BillServicel.getOwnerBillAllCount(params);
        result.put(getOwnerBillAllCount);
        return result;
    }

    /**
     * 功能描述: 
     * 根据业主手机号获取业主合同，然后获取业主账单资料
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/1 14:15
     */
    @GetMapping("/getOwnerBillList")
    public JsonResult getOwnerBillList(@RequestParam Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        JsonResult<Map<String, Object>> jsonResult = HouseFeign.getOwnerBillList(params);
        if (jsonResult.isSuccess() == false) {
            params.put("owdealList",new ArrayList<String>());
        }
        params.putAll(jsonResult.getResult());
        params.put("BiOutType","1");//账单类型  1.业主   2.供应商  3.退租账单   4.公共事业费

        Integer cout=BillServicel.getOwnerBillListCount(params);
        if(cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>> getOwnerBillList=BillServicel.getOwnerBillList(params);
        result=pageUtils.getJsonResult(getOwnerBillList, params, cout);
        return result;
    }

    /**
     * 功能描述: 
     * 根据租客编号和出租类型获取租客所有账单
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/4 21:49
     */
    @GetMapping("/getBillAllList")
    public JsonResult getBillAllList(@RequestParam Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        params.put("delfg","0");
//        params.put("FirstMon","0");//房租账单是否是首月    0否   1.是
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout=BillServicel.getBillAllListCount(params);
        if(cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>> getBillAllList=BillServicel.getBillAllList(params);
        result=pageUtils.getJsonResult(getBillAllList, params, cout);
        return result;
    }

    /**
     * 功能描述: 
     * 根据账单编号和租客编号获取账单详情
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/6 15:54
     */
    @GetMapping("/getBillAllBiIdMap")
    public JsonResult getBillAllBiIdMap(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        params.put("delfg","0");
        params.put("FirstMon","0");//房租账单是否是首月    0否   1.是
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"BiId"})){
            result.putFailed("BiId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"TenId"})){
            result.putFailed("TenId不能为空！");
            return  result;
        }
        result.put(BillServicel.getBillAllBiIdMap(params));
        return result;
    }

    /**
     * 功能描述: 
     * 根据合同编号获取账单
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/15 11:59
     */
    @PostMapping("/getBillFeeMapFile")
    public JsonResult getBillFeeMapFile(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"Source"})){
            result.putFailed("Source不能为空！");
            return  result;
        }
        result.put(BillServicel.getBillFeeMapFile(params));
        return result;
    }

    /**
     * 功能描述: 
     * 支付宝水电燃支付，审核之后重新支付
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/16 14:28
     */
    @PostMapping("/UpdateBillPay")
    public JsonResult UpdateBillPay(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        params.put("BiType","1");//房租
        params.put("DepositType","2");//房租类型   1.房租房租押金   2.水电押金   (账单类型为1)
        params.put("FirstMon","1");//房租账单是否是首月    0否   1.是
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"PayId"})){
            result.putFailed("PayId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"PaId"})){
            result.putFailed("PaId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"TenId"})){
            result.putFailed("TenId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"PayFile"})){
            result.putFailed("PayFile不能为空！");
            return  result;
        }
        return BillServicel.UpdateBillPay(params);
    }

    /**
     * 功能描述: 
     * 根据账单资料生成账单支付信息
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/29 20:25
     */
    @PostMapping("/setPayWechatBill")
    public JsonResult setPayWechatBill(@RequestBody Map<String,Object>params)throws Exception{
        JsonResult result=new JsonResult();
        params.put("delfg","0");
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"BiId"})){
            result.putFailed("BiId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"uopid"})){
            result.putFailed("uopid不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"remoteIp"})){
            result.putFailed("remoteIp不能为空！");
            return  result;
        }
        result.put(BillServicel.setPayWechatBill(params));
        return result;
    }

    /**
     * 功能描述: 
     * 微信账单完成支付验证
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/30 10:18
     */
    @PostMapping("/PayWechatBillCensor")
    public JsonResult PayWechatBillCensor(@RequestBody Map<String,Object>params)throws Exception{
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"BiId"})){
            return JsonResult.error("BiId不能为空！");
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"TenId"})){
            return  JsonResult.error("TenId不能为空！");
        }
        return BillServicel.PayWechatBillCensor(params);
    }

    /**
     * 获取首月账单
     * @param params
     * @return
     */
    @PostMapping("/getFirstRoomGEW")
    public JsonResult getFirstRoomGEW(@RequestBody Map<String,Object> params){
        JsonResult result = new JsonResult();
        return result.put(BillServicel.getFirstRoomGEW(params));
    }

    /**
     * 增加支付信息
     * @param params
     * @return
     */
    @PostMapping("/AddBillPay")
    public JsonResult AddBillPay(@RequestBody Map<String,Object> params){
        JsonResult result = new JsonResult();
        Map<String,Object> map = new HashMap<>();
        map.put("CountPay",BillServicel.AddBillPay(params));
        return  result.put(map);
    }

    /**
     * 修改账单信息
     * @param params
     * @return
     */
    @PostMapping("/UpdateBill")
    public JsonResult UpdateBill(@RequestBody Map<String,Object> params){
        JsonResult result = new JsonResult();
        Map<String,Object> map = new HashMap<>();
        map.put("CountPay",BillServicel.UpdateBill(params));
        return  result.put(map);

    }

    /**
     * 保存微信支付订单
     * @param params
     * @return
     */
    @PostMapping("/AddPayWechat")
    public JsonResult AddPayWechat(@RequestBody Map<String,Object> params){
        JsonResult result = new JsonResult();
        Map<String,Object> map = new HashMap<>();
        map.put("CountWechat",BillServicel.AddPayWechat(params));
        return  result.put(map);

    }

    /**
     * 根据订单号查询关联账单编号
     * @param params
     * @return
     */
    @PostMapping("/getBillPay")
    public JsonResult  getBillPay(@RequestBody Map<String,Object> params){
       JsonResult result = new JsonResult();
       return result.put(BillServicel.getBillPay(params));
    }



    /**
     * 根据订单号查询关联账单编号
     * @param params
     * @return
     */
    @PostMapping("/addTenantBill")
    public JsonResult  addTenantBill(@RequestBody Map<String,Object> params){
        JsonResult result = new JsonResult();
        Map<String,Object> map = new HashMap<>();
        map.put("CountTenBill",BillServicel.addTenantBill(params));
        return  result.put(map);
    }


    @GetMapping("/getBillPays")
    public JsonResult getBillPays(@RequestParam Map<String,Object> params){
        return BillServicel.getBillPays(params);
    }


    @GetMapping("/geiTenBill")
    public JsonResult geiTenBill(@RequestParam Map<String,Object> params){
        return BillServicel.geiTenBill(params);
    }


    @PostMapping("/UpdateWechat")
    public JsonResult UpdateWechat(@RequestBody Map<String,Object> params){
        JsonResult result = new JsonResult();
        Map<String,Object> map = new HashMap<>();
        map.put("CountWechat",BillServicel.UpdateWechat(params));
        return  result.put(map);
    }


}
